package com.zyg.controller;

import com.zyg.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/datas")
public class DataSController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/tolist")
    public String tolist(Model model) {
        // 可以在这里添加一些初始化数据，但不需要处理具体表的数据
        return "data_screen/datasc";
    }

    @RequestMapping("/getGradeDistribution")
    @ResponseBody
    public List<Map<String, Object>> getGradeDistribution() {
        // 获取学生年级分布数据
        List<Object[]> rawGradeDistribution = studentService.getGradeDistribution();

        // 将数据转换为 List<Map<String, Object>>
        List<Map<String, Object>> gradeDistribution = rawGradeDistribution.stream()
                .map(row -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("grade", row[0]);
                    map.put("count", row[1]);
                    return map;
                })
                .collect(Collectors.toList());

        return gradeDistribution;
    }

    @RequestMapping("/getStudentTotalCount")
    @ResponseBody
    public Map<String, Integer> getStudentTotalCount() {
        // 获取学生总人数
        long totalCount = studentService.count();
        // 返回包含总人数的 Map
        Map<String, Integer> result = new HashMap<>();
        result.put("totalCount", (int) totalCount);
        return result;
    }

    // 可以为其他表的数据添加类似的处理方法
//    @RequestMapping("/getAnotherTableData")
//    @ResponseBody
//    public List<Map<String, Object>> getAnotherTableData() {
//        // 获取其他表的数据
//        List<Object[]> rawData = studentService.getAnotherTableData();
//
//        // 将数据转换为 List<Map<String, Object>>
//        List<Map<String, Object>> data = rawData.stream()
//                .map(row -> {
//                    Map<String, Object> map = new HashMap<>();
//                    map.put("column1", row[0]);
//                    map.put("column2", row[1]);
//                    // 添加其他列
//                    return map;
//                })
//                .collect(Collectors.toList());
//
//        return data;
//    }
}
